package io.renren.common.utils;

import io.renren.modules.sp.dto.GradeExport;
import io.renren.modules.sp.dto.MatchItemLimitExport;
import io.renren.modules.sp.entity.SpMatchAreaLimitEntity;
import java.util.*;
import java.util.stream.Collectors;

public class DynamicHeadUtil {

    /**
     * 构建动态表头
     * @param matchItemIds 赛项ID列表
     * @return 动态表头列表
     */
    public static List<List<String>> buildDynamicHeaders(List<String> matchItemIds) {
        List<List<String>> headers = new ArrayList<>();
        // 固定表头 - 省份
        headers.add(Collections.singletonList("省份"));
        // 动态表头 - 每个赛项的人数限制
        for (String matchItemId : matchItemIds) {
            headers.add(Collections.singletonList(matchItemId+""));
        }
        return headers;
    }

    /**
     * 构建动态数据(修正为List<List<Object>>格式)
     * @param dataList 原始数据
     * @param matchItemIds 赛项ID列表
     * @return 动态数据列表
     */
    public static List<List<Object>> buildDynamicData(
            List<SpMatchAreaLimitEntity> dataList,
            List<String> matchItemIds) {
        // 按省份分组
        Map<String, Map<String, Integer>> provinceDataMap = dataList.stream()
                .collect(Collectors.groupingBy(
                        SpMatchAreaLimitEntity::getProvince,
                        Collectors.toMap(
                                SpMatchAreaLimitEntity::getMatchItemName,
                                SpMatchAreaLimitEntity::getNum,
                                (oldValue, newValue) -> oldValue
                        )
                ));
        // 转换为EasyExcel需要的格式(List<List<Object>>)
        List<List<Object>> result = new ArrayList<>();
        for (String province : provinceDataMap.keySet()) {
            List<Object> rowData = new ArrayList<>();
            // 添加省份
            rowData.add(province);
            Map<String, Integer> itemData = provinceDataMap.get(province);
            // 添加每个赛项的人数限制
            for (String matchItemId : matchItemIds) {
                rowData.add(itemData.getOrDefault(matchItemId, 0));
            }
            result.add(rowData);
        }
        return result;
    }

    public static List<MatchItemLimitExport> convertLimitData(List<Map<String, Object>> dataList, List<String> itemNames) {
        Map<String, MatchItemLimitExport> resultMap = new LinkedHashMap<>();
        // 初始化所有省份数据
        dataList.forEach(row -> {
            String province = (String) row.get("province");
            resultMap.computeIfAbsent(province, k -> {
                MatchItemLimitExport data = new MatchItemLimitExport();
                data.setId((Integer) row.get("id"));
                data.setMatchName((String) row.get("matchName"));
                data.setProvince(province);
                return data;
            });
        });
        // 填充赛项数据
        dataList.forEach(row -> {
            String province = (String) row.get("province");
            String itemName = (String) row.get("itemName");
            Integer num = (Integer) row.get("num");
            MatchItemLimitExport data = resultMap.get(province);
            data.getItemValues().put(itemName, num);
        });
        // 确保所有赛项都有值（0表示无数据）
        resultMap.values().forEach(data -> {
            itemNames.forEach(itemName -> {
                data.getItemValues().putIfAbsent(itemName, 0);
            });
        });
        return new ArrayList<>(resultMap.values());
    }

    public static List<GradeExport> convertData(List<Map<String, Object>> dbResults) {
        // 使用Map来按省份分组数据
        Map<String, GradeExport> provinceMap = new HashMap<>();
        for (Map<String, Object> row : dbResults) {
            String province = (String) row.get("province");
            String grade = (String) row.get("grade");
            Integer count = ((Long) row.get("total")).intValue();
            GradeExport data = provinceMap.computeIfAbsent(province, k -> {
                GradeExport newData = new GradeExport();
                newData.setId(Integer.parseInt(row.get("id")+""));
                newData.setMatchName((String) row.get("matchName"));
                newData.setProvince(province);
                return newData;
            });
            switch (grade) {
                case "一等奖":
                    data.setFirstPrize(count);
                    break;
                case "二等奖":
                    data.setSecondPrize(count);
                    break;
                case "三等奖":
                    data.setThirdPrize(count);
                    break;
                case "优秀奖":
                    data.setExcellentPrize(count);
                    break;
            }
        }
        return new ArrayList<>(provinceMap.values());
    }
}
